﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rabbit.Model;

namespace Rabbit.BusinessLogicLayer
{
    public class UserFollowerListBL : AutoUpdateMessageClassBase<User>
    {
        public long UserID { get; set; }
        public UserFollowerListBL()
            : base(double.MaxValue)
        {

        }
        protected override void FetchNewMessage()
        {
            throw new NotImplementedException();
        }

        protected override void FetchMessage()
        {
            throw new NotImplementedException();
        }

        private Users GetFollowerList(long cursorID)
        {
            string tempString = RequestHelper.SendOAuthRequest(
            string.Format("statuses/followers/{0}.json", UserID), "cursor=" + cursorID + "&count=" + Utils.Configuration.PageSize);

            Users statuses = WrappedJsonHelper.Deserialize<Users>(tempString);
            return statuses;
        }

        public override List<User> GetNextPageMessage()
        {
            Users tempUsers = GetFollowerList(NextPageCursorID);

            //push previouse page cursor id to the queue
            CursorQueue.Push(CurrentPageCursorID.Value);
            CurrentPageCursorID = NextPageCursorID;
            FetchedMessageCount = tempUsers.UserList.Count;

            //set next page cursor id to the returned value
            NextPageCursorID = tempUsers.CursorID;
            return tempUsers.UserList;
        }

        public override List<User> GetPreviousePageMessage()
        {
            if (CursorQueue.Count == 0)
            {
                return null;
            }

            CurrentPageCursorID = CursorQueue.Pop();

            Users tempUsers = GetFollowerList(CurrentPageCursorID.Value);
            NextPageCursorID = tempUsers.CursorID;
            FetchedMessageCount = tempUsers.UserList.Count;
            return tempUsers.UserList;
        }

        public override List<User> GetFirstPageMessage()
        {
            CursorQueue.Clear();

            Users tempUsers = GetFollowerList(-1);
            NextPageCursorID = tempUsers.CursorID;
            CurrentPageCursorID = -1;
            FetchedMessageCount = tempUsers.UserList.Count;

            return tempUsers.UserList;
        }
    }
}
